import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/view/login.vue'
import Main from "@/view/main.vue"
import NotFound from "@/view/notfound.vue"
import Register from "@/view/register.vue"
import Home from "@/components/home.vue"
import PerInfo from "@/components/perInfo.vue"
import ProFiles from "@/view/profiles.vue"
Vue.use(Router)

const router= new Router({
  mode:"history",
  routes: [
    {
      path: '/',
      redirect:"/login"
    },
    {
      path:"/login",
      name:"login",
      component:Login
    },
    {
      path:"/main",
      name:"Main",
      component:Main,
      children:[
        {
          path:"",
          name:"home",
          component:Home
        },
        {
          path:"home",
          name:"home",
          component:Home
        },
        {
          path:"perinfo",
          name:"PerInfo",
          component:PerInfo
        },
        {
          path:"profiles",
          name:"ProFiles",
          component:ProFiles
        }
      ]
    },
    {
      path:"*",
      name:"NotFound",
      component:NotFound
    },
    {
      path:"/register",
      name:"Register",
      component:Register
    }
  ]
});

// to: Route: 即将要进入的目标 路由对象

// from: Route: 当前导航正要离开的路由

// next: Function: 一定要调用该方法来 resolve 这个钩子。执行效果依赖 next 方法的调用参数。

// next(): 进行管道中的下一个钩子。如果全部钩子执行完了，则导航的状态就是 confirmed (确认的)。

router.beforeEach((to,from,next)=>{
  const isLogin=localStorage.getItem("eletoken") ? true : false;
  if(to.path=="/login"||to.path=="/register"){
    next();
  }else{
    isLogin ? next(): next("/login");
  }
});

export default router;